package com.xinjiang.electricpower.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.xinjiang.electricpower.domain.DTO.PsmWorkOrderDTO;
import com.xinjiang.electricpower.domain.DTO.VoucherDTO;
import com.xinjiang.electricpower.domain.vo.PsmWorkOrderDetailVO;
import com.xinjiang.electricpower.domain.vo.PsmWorkOrderVO;
import com.xinjiang.electricpower.service.IPsmWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feizhao
 * @description 工单中心-派工单控制层类
 * @since 2022/11/9
 */
@RestController
@RequestMapping("/xinjiang-power-supply-station/repair-order-center/")
@Api(value = "工单中心-派工单", tags = "工单中心-派工单")
@Validated
@Slf4j
@RequiredArgsConstructor
public class PsmWorkOrderController extends BaseController {

    private final IPsmWorkOrderService psmWorkOrderService;



    /**
     * @description 条件查询并分页派工单
     * @param psmWorkOrderDTO 派工单实体类
     * @param page              分页对象
     * @return R<PageInfo<PsmWorkOrderVO>> 工单列表
     */
    @GetMapping("/dispatch-order/list")
    @ApiOperation(value = "条件查询并分页派工单", notes = "条件查询并分页派工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "psmWorkOrderDTO", value = "派工单DTO实体类", paramType = "query",dataType="PsmWorkOrderDTO",dataTypeClass = com.xinjiang.electricpower.domain.DTO.PsmWorkOrderDTO.class),
            @ApiImplicitParam(name = "page", value = "分页对象", paramType = "query",dataType="PageDTO<PsmWorkOrderVO>",dataTypeClass =com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO.class)
    })
    public R<PageInfo<PsmWorkOrderVO>> getXjOrderGeneralProcess(PsmWorkOrderDTO psmWorkOrderDTO, PageDTO<PsmWorkOrderVO> page) {
        PageInfo<PsmWorkOrderVO> pages = psmWorkOrderService.selectPsmWorkOrdersPage(page, psmWorkOrderDTO);
        return R.ok(pages, "查询成功");
    }

    /**
     * @return R<Long> 工单总数
     * @description 查询派工单总数
     */
    @GetMapping("/dispatch-order/count")
    @ApiOperation(value = "查询派工单总数", notes = "查询派工单总数")
    public R<Map<String,Long>> getOrderNum() {
        Map<String,Long> map= new HashMap<String,Long>();
        map.put("dispatchOrderNums",psmWorkOrderService.countDispatchWorkOrder());
        return R.ok(map, "查询成功");
    }
    /**
     * @return R<Object> 返回结果
     * @description 制单/新增派工单
     */
    @PostMapping("/dispatch-order/voucher")
    @Log(title = "制单", businessType = BusinessType.INSERT)
    @ApiOperation(value = "制单", notes = "制单")
    public R<Object> voucher(@RequestBody VoucherDTO voucherDTO) {
        boolean flag = psmWorkOrderService.insertPsmWorkOrder(voucherDTO);
        if(flag){
        return R.ok();
        }
        return R.fail("制单失败");
    }
    /**
     * @return R<Long> 返回结果
     * @description 制单/更新派工单
     */
    @PutMapping("/dispatch-order/statement")
    @Log(title = "签单/转派/结单", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "签单/转派/结单", notes = "签单/转派/结单")
    public R<Object> statement(@RequestBody VoucherDTO voucherDTO) {
        boolean flag = psmWorkOrderService.updatePsmWorkOrder(voucherDTO);
        if(flag){
            return R.ok();
        }
        return R.fail("操作失败");
    }
    /**
     * 导出待处理、已完成、处理中的工单
     */
    @ApiOperation("批量导出派工单")
    @Log(title = "批量导出派工单", businessType = BusinessType.EXPORT)
    @PostMapping("/dispatch-order/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "psmWorkOrderDTO", value = "psmWorkOrderDTO的属性批量ids", required = true, paramType = "body",dataType = "PsmWorkOrderDTO",dataTypeClass = java.lang.String.class),
            @ApiImplicitParam(name = "response", value = "响应流",dataType ="HttpServletResponse",dataTypeClass=javax.servlet.http.HttpServletResponse.class)
    })
    public void export(HttpServletResponse response, @RequestBody PsmWorkOrderDTO psmWorkOrderDTO) {
        List<PsmWorkOrderVO> list = psmWorkOrderService.selectPsmWorkOrders(psmWorkOrderDTO.getIds());
        ExcelUtil<PsmWorkOrderVO> util = new ExcelUtil<PsmWorkOrderVO>(PsmWorkOrderVO.class);
        util.exportExcel(response, list, "派工单");
    }
    /**
     * 派工单详情
     */
    @ApiOperation("派工单详情")
    @Log(title = "派工单详情")
    @GetMapping("/dispatch-order/detail/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "派工单id", dataType = "string", paramType = "path",required = true,dataTypeClass = java.lang.String.class),
    })
    public R<PsmWorkOrderDetailVO> getPsmWorkOrder( @NotNull(message = "id不能为null") @PathVariable String id) {
        PsmWorkOrderDetailVO psmWorkOrderDetailVO = psmWorkOrderService.selectPsmWorkOrderDetail(id);
        return R.ok(psmWorkOrderDetailVO,"查询成功");
    }
    /**
     * 删除未签发派工单
     */
    @ApiOperation("删除未签发派工单")
    @Log(title = "删除未签发派工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/dispatch-order/remove")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "psmWorkOrderDTO", value = "psmWorkOrderDTO的属性派工单id", paramType = "body",dataType = "PsmWorkOrderDTO",dataTypeClass = com.xinjiang.electricpower.domain.DTO.PsmWorkOrderDTO.class),

    })
    public R<Object> remove(@RequestBody PsmWorkOrderDTO psmWorkOrderDTO) {
        Boolean flag=psmWorkOrderService.removePsmWorkOrders(psmWorkOrderDTO.getId());
        if(Boolean.TRUE.equals(flag)){
            return R.ok(true,"删除成功");
        }
        return R.fail("删除失败,派工单状态必须为待签发");
    }
    /**
     * 导出派工单，全导出或条件查询导出
     */
    @ApiOperation("导出派工单，全导出或条件查询导出")
    @Log(title = "导出派工单，全导出或条件查询导出", businessType = BusinessType.EXPORT)
    @PostMapping("/dispatch-order/exports")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "psmWorkOrderDTO", value = "工单实体类", paramType = "body",dataType="PsmWorkOrderDTO",dataTypeClass =com.xinjiang.electricpower.domain.DTO.PsmWorkOrderDTO.class ),
            @ApiImplicitParam(name = "response", value = "响应流",dataType ="HttpServletResponse",dataTypeClass=javax.servlet.http.HttpServletResponse.class)
    })
    public void exports(HttpServletResponse response, @RequestBody PsmWorkOrderDTO psmWorkOrderDTO) {

        List<PsmWorkOrderVO> list = psmWorkOrderService.selectPsmWorkOrders(psmWorkOrderDTO);
        ExcelUtil<PsmWorkOrderVO> util = new ExcelUtil<PsmWorkOrderVO>(PsmWorkOrderVO.class);
        util.exportExcel(response, list, "派工单");
    }
}
